﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace Tum.CollabXT.VModellXT.Tailoring
{
    public class VModellXT
    {
        public readonly string ModelPath;
        private readonly XmlDocument ModelDocument;

        internal const string ModelDocumentName = "V-Modell-XT.xml";
        internal const string TemplateTextsDocumentName = "V-Modell-XT-Mustertexte.xml";
        internal const string ProjectSchemaName = "V-Modell-XT-Projekt.xsd";
        internal const string XmlSchemaName = "xml.xsd";
        internal const string ModelSchemaName = "V-Modell-XT-Metamodell.xsd";


        public string TemplateTextsDocumentPath
        {
            get
            {
                return GetFileExistsPath(ModelPath + "/" + TemplateTextsDocumentName);
            }
        }

        public string ModelDocumentPath
        {
            get
            {
                return GetFileExistsPath(ModelPath + "/" + ModelDocumentName);
            }
        }

        public string ProjectSchemaPath
        {
            get
            {
                return GetFileExistsPath(ModelPath + "/" + ProjectSchemaName);
            }
        }

        public string ModelSchemaPath
        {
            get
            {
                return GetFileExistsPath(ModelPath + "/" + ModelSchemaName);
            }
        }

        public string XmlSchemaPath
        {
            get
            {
                return GetFileExistsPath(ModelPath + "/" + XmlSchemaName);
            }
        }
        


        public ProjectType[] ProjectTypes
        {
            get
            {
                if (_ProjectTypes == null)
                {
                    List<ProjectType> prTypes = new List<ProjectType>();
                    XmlNodeList ptList = ModelDocument.SelectNodes("//Projekttyp");
                    foreach (XmlNode ptNode in ptList)
                        prTypes.Add(new ProjectType(ptNode));
                    _ProjectTypes = prTypes.ToArray();
                }
                return _ProjectTypes;
            }
        }
        ProjectType[] _ProjectTypes;


        public VModellXT(string modelPath)
        {
            if (modelPath == null)
                throw new ArgumentNullException("modelPath");
            if (!Directory.Exists(modelPath))
                throw new ArgumentException("Model directory does not exist.", "modelPath");
            ModelPath = modelPath;

            ModelDocument = new XmlDocument();
            ModelDocument.Load(ModelPath + "/" + ModelDocumentName);
        }

        /// <summary>
        /// Return file path if it exists and null if not.
        /// </summary>
        private static string GetFileExistsPath(string path)
        {
            if (File.Exists(path))
                return path;
            else
                return null;
        }
    }
}
